package research.sbookmarking.process.util;

import java.util.Arrays;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.TimeZone;

//import org.connotea.Bookmark;
//import org.connotea.Post;
import org.connotea.Tag;

//import research.sbookmarking.AppController;

public class StringUtils {

	public static String convertToString (Object item){
		String obj = null;
		
		if (item.getClass().getName().equals("org.connotea.Tag")){
			obj = ((Tag)item).getValue();
		}
/*
		else if (item.getClass().getName().equals("org.connotea.Bookmark")){
			obj = ((Bookmark)item).getLink().toString();
		}
		else if (item.getClass().getName().equals("org.connotea.Post")){
			obj = ((Post)item).getCreator();
		}
*/
		else if (item.getClass().getName().equals("java.util.Date")){
			java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
			TimeZone gmtTime = TimeZone.getTimeZone("GMT");
			format.setTimeZone(gmtTime);
			obj = format.format((java.util.Date)item);
		}
		else {
			obj = item.toString();
		}
		
		return obj;
	}

	public static List<String> convert(Iterator<?> items){
		List<String> list = new ArrayList<String>(1);
		while(items.hasNext()){
			list.add(convertToString(items.next()));
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static Object convert (Object obj){
		Object value = null;
		
		List<Class<?>> classes = Arrays.asList(obj.getClass().getInterfaces());
		if (StringUtils.contains("java.util.List",classes.iterator())) {
			value = convert(((List)obj).iterator());
		}
		else {
			value = convertToString(obj);
		}
		
		return value;
	}
	
	@SuppressWarnings("unchecked")
	public static List convertToList (Object obj){
		List value = new ArrayList(1);
		
		List<Class<?>> classes = Arrays.asList(obj.getClass().getInterfaces());
		if (StringUtils.contains("java.util.List",classes.iterator())) {
			value = convert(((List)obj).iterator());
		}
		else {
			value.add(convertToString(obj));
		}
		
		return value;
	}
	
	//@SuppressWarnings("all")
	public static boolean contains (String className, Iterator<?> classes){
		while (classes.hasNext()) {
			if (((Class<?>)classes.next()).getName().equals(className)) {
				return true;
			}
		}
		return false;
	}

	public static String convertList (Iterator<?> items){
		StringBuffer temp = new StringBuffer();

		while (items.hasNext()){
			String anObj = convertToString(items.next());
			if (items.hasNext()) {
				temp.append(anObj + ", ");
			}
			else {
				temp.append(anObj);
			}
		}
		
		return temp.toString();
	}
	
	@SuppressWarnings("unchecked")
	public static String convertCVS (Object obj){
		StringBuffer value = new StringBuffer();

		List<Class<?>> classes = Arrays.asList(obj.getClass().getInterfaces());
		if (StringUtils.contains("java.util.List",classes.iterator())) {
			value.append(convertList(((List)obj).iterator()));
			value.append("\t "+((List)obj).size());
		}
		else if(obj.getClass().getName().equals("java.util.TreeMap")){
			value.append(((TreeMap)obj).size());
		}
		else {
			value.append(obj);
		}
		return value.toString();
	}
	
	@SuppressWarnings("unchecked")
	public static ArrayList<Object> convertExcel (Object obj){
		ArrayList value = new ArrayList(1);

		List<Class<?>> classes = Arrays.asList(obj.getClass().getInterfaces());
		if (StringUtils.contains("java.util.List",classes.iterator())) {
			value.add(convertList(((List)obj).iterator()));
			value.add(Double.valueOf(((List)obj).size()));
		}
		else if(obj.getClass().getName().equals("java.util.TreeMap")){
			value.add(((TreeMap)obj).size());
		} 
		else if(obj.getClass().getName().equals("java.lang.Double")||
				obj.getClass().getName().equals("java.lang.Integer")){
			value.add(obj);
		}
		else {
			value.add(obj.toString());
		}
		return value;
	}
	
	public static String convert (String message){
		if (message == null){
			return new String();
		}
		else {
			return message.replaceAll("\n", " ");
		}
	}

	public static String convertIllegalChar (String param){
		String temp = param.replace('/', '_').replace(':', '_').replace('?','_').replace('|','_');
		return temp.replace('\\','_').replace('*','_').replace('<','_').replace('>','_');
	}
	
	/*
	@SuppressWarnings("unchecked")
	public static void convert(TreeMap TreeMap){
		ArrayList keys = new ArrayList(TreeMap.keySet());
		
		for (int count = 0; count<keys.size();count++){
			List values = (List)TreeMap.get(keys.get(count));
			TreeMap.put(keys.get(count), StringUtils.convertList(values.iterator()));
		}
	}
	*/

}
